using System;
using System.Collections.Generic;
using System.Text;

namespace Gestion.Lib.Data
{
    public abstract class HibernateDao<T, ID> : HibernateReadOnlyDao<T, ID>
    {
        /// <summary>
        /// Refresh an entity
        /// </summary>
        /// <param name="entity"></param>
        public void Refresh(T entity)
        {
            HibernateTemplate.Refresh(entity);
        }

        /// <summary>
        /// Saves an entity
        /// </summary>
        /// <param name="entity"></param>
        public void Save(T entity)
        {
            //PopulateMetaData(entity, true);
            HibernateTemplate.Save(entity);
        }

        /// <summary>
        /// Saves or updates an entity
        /// </summary>
        /// <param name="entity"></param>
        public void SaveOrUpdate(T entity)
        {   
            //PopulateMetaData(entity, false);
            HibernateTemplate.SaveOrUpdate(entity);
        }

        /// <summary>
        /// Merges an entity with current session's state, 
        /// or saveOrUpdates it if there's no state for it 
        /// in current session.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Merge(T entity)
        {
            //PopulateMetaData(entity, false);
            return (T)HibernateTemplate.SaveOrUpdateCopy(entity);
        }

        /// <summary>
        /// Updates an entity
        /// </summary>
        /// <param name="entity"></param>
        public void Update(T entity)
        {
            //PopulateMetaData(entity, false);
            HibernateTemplate.Update(entity);
        }

        /// <summary>
        /// Deletes an entity
        /// </summary>
        /// <param name="entity"></param>
        public void Delete(T entity)
        {
            HibernateTemplate.Delete(entity);
        }

        /// <summary>
        /// Deletes an entity by ID
        /// </summary>
        /// <param name="id"></param>
        public void DeleteById(ID id)
        {
            T entity = GetById(id);
            if (entity != null)
            {
                Delete(entity);
            }
        }
    }

}
